package com.sinosoft.msf.base.supplier.util;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.sinosoft.msf.base.supplier.expection.BizException;

import java.util.function.Function;

/**
 * @author lid
 */
public class PageUtil {
    public static <T,R> Page<R> transPage(Page<T> paramPage, Function<T,R> fuc) {
        Page<R> resPage = new Page<>();
        resPage.setPages(paramPage.getPages());
        resPage.setPageSize(paramPage.getPageSize());
        resPage.setTotal(paramPage.getTotal());
        for (T t : paramPage) {
            resPage.add(fuc.apply(t));
        }
        return resPage;
    }
    public static <T,R> Page<R> transPage(Page<T> paramPage, Class<R> clazz) {
        R r;
        try {
            r = clazz.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            throw new BizException(e);
        }
        Page<R> resPage = new Page<>();
        resPage.setPages(paramPage.getPages());
        resPage.setPageSize(paramPage.getPageSize());
        resPage.setTotal(paramPage.getTotal());
        for (T t : paramPage) {
            BeanUtil.copyProperties(t, r);
            resPage.add(r);
        }
        return resPage;
    }
}
